package com.jackaltsc.mydemodatastorage.sqlite;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jackaltsc.mydemodatastorage.R;

public class SQLiteActivity extends Activity implements OnClickListener {

    private Button btnCreate;
    private Button btnUserAdd, btnUserDelete, btnUserUpdate;
    private Button btnUserQueryOne, btnUserQueryMul;

    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_sqlite);

        initView();
    }

    private void initView() {

        btnCreate = (Button) findViewById(R.id.btn_tb_create);

        btnUserAdd = (Button) findViewById(R.id.btn_user_add);
        btnUserDelete = (Button) findViewById(R.id.btn_user_delete);
        btnUserUpdate = (Button) findViewById(R.id.btn_user_update);

        btnUserQueryOne = (Button) findViewById(R.id.btn_user_queryone);
        btnUserQueryMul = (Button) findViewById(R.id.btn_user_querymul);

        tvResult = (TextView) findViewById(R.id.tv_result);

        initEvent();

    }

    private void initEvent() {

        btnCreate.setOnClickListener(this);
        btnUserAdd.setOnClickListener(this);
        btnUserDelete.setOnClickListener(this);
        btnUserUpdate.setOnClickListener(this);
        btnUserQueryOne.setOnClickListener(this);
        btnUserQueryMul.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btn_tb_create:
                funTableCreate();
                break;

            case R.id.btn_user_add:
                funUserAdd();
                break;

            case R.id.btn_user_delete:
                funUserDelete();
                break;

            case R.id.btn_user_update:
                funUserUpdate();
                break;

            case R.id.btn_user_queryone:
                funUserQueryOne();
                break;

            case R.id.btn_user_querymul:
                funUserQueryMul();
                break;
        }

    }

    private void funUserQueryMul() {

        UserDao mUserDao = new UserDaoImp(this);
        List<User> userList = mUserDao.userQueryMul();

        String result = "";

        for (int i = 0; i < userList.size(); i++) {
            result += userList.get(i).toString();
        }

        tvResult.setText(result);

    }

    private void funUserQueryOne() {

        UserDao mUserDao = new UserDaoImp(this);
        User mUser = mUserDao.userQueryOne(4);

        String result = mUser.toString();

        tvResult.setText(result);
    }

    private void funUserUpdate() {

        UserDao mUserDao = new UserDaoImp(this);

        User mUser = new User(10, "jjjjj", "jjjjjpwd");

        boolean flag = mUserDao.userUpDateOne(mUser);

        if (flag) {
            Toast.makeText(SQLiteActivity.this, "更新成功！", Toast.LENGTH_SHORT).show();
        }
    }

    private void funUserDelete() {

        UserDao mUserDao = new UserDaoImp(this);

        boolean flag = mUserDao.userDeleteOne(5);

        if (flag) {
            Toast.makeText(SQLiteActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(SQLiteActivity.this, "删除失败！", Toast.LENGTH_SHORT).show();
        }

    }

    private void funUserAdd() {

        UserDao mUserDao = new UserDaoImp(this);

        User mUser = new User(4, "你好", "sssss");

        boolean flag = mUserDao.userAddOne(mUser);

        if (flag) {
            Toast.makeText(SQLiteActivity.this, "添加成功！", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(SQLiteActivity.this, "添加失败！", Toast.LENGTH_SHORT).show();
        }

    }

    private void funTableCreate() {
        SQLiteHelper mSQLiteHelper = new SQLiteHelper(this);
        mSQLiteHelper.getWritableDatabase();
    }
}
